package com.hethongviet.vending.pages;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hethongviet.vending.AppMain;
import com.hethongviet.vending.Constant;
import com.hethongviet.vending.AppConfig;
import com.google.android.DemoKit.R;
import com.hethongviet.vending.Utils;
import com.hethongviet.vending.helper.CodeResult;
import com.hethongviet.vending.helper.TimerOutUtils;
import com.hethongviet.vending.helper.dialog.DialogMessageBack;
import com.hethongviet.vending.helper.dialog.DialogMessageConfirm;

public class InputTextActivity extends Activity
{
	public static final String KEY_INPUT_TYPE = "input_type";
	public static final String KEY_INPUT_DATA = "input_data";

	public static final int INPUT_PRICE = 0;
	public static final int INPUT_PRODUCT_AMOUNT = INPUT_PRICE + 1;
	public static final int INPUT_PRODUCT_AMOUNT_DESC = INPUT_PRODUCT_AMOUNT + 1;
	public static final int INPUT_CHANGE_PRODUCT = INPUT_PRODUCT_AMOUNT_DESC + 1;
	public static final int INPUT_ADD_PRODUCT = INPUT_CHANGE_PRODUCT + 1;
	public static final int INPUT_SERVER_USERNAME = INPUT_ADD_PRODUCT + 1;
	public static final int INPUT_SERVER_PASSWORD = INPUT_SERVER_USERNAME + 1;
	public static final int INPUT_MACHINE_CODE = INPUT_SERVER_PASSWORD + 1;
	public static final int INPUT_DEVICE_NUMBER = INPUT_MACHINE_CODE + 1;
	public static final int INPUT_USER_AMOUNT = INPUT_DEVICE_NUMBER + 1;
	public static final int INPUT_SERVER_DATABASE = INPUT_USER_AMOUNT + 1;

	private EditText _textInput = null;
	private Button _buttonOK = null;
	private String _valueInput = "";
	private TextView _textTitle = null;
	private TextView _textErrorNumber = null;

	private TimerOutUtils _timerOutUtils = null;
	private int _numberFinish = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		/* Full screen */
		Utils.prepareWindow(this);

		_numberFinish = getIntent().getIntExtra(Constant.EXTRA_FINISH, 2);

		setContentView(R.layout.page_input_data);

		findViewById(R.id.button_dialog_cancel).setOnClickListener(
				new OnClickListener()
				{

					@Override
					public void onClick(View arg0)
					{
						// OnClickListener onClickListenerYES = new
						// OnClickListener() {
						//
						// @Override
						// public void onClick(View v)
						// {
						// AppMain.getInstance().hideAllDialog();
						setResult(RESULT_CANCELED);
						finish();
						// }
						// };
						//
						// AppMain.getInstance().showAlert(InputTextActivity.this,
						// R.string.message_exit_confirm_service, false,
						// onClickListenerYES, false);
					}
				});

		_textTitle = (TextView) findViewById(R.id.text_input_title);

		((TextView) findViewById(R.id.text_user_name)).setVisibility(View.GONE);

		/* Text has input type default as number */
		_textInput = (EditText) findViewById(R.id.text_dialog_input);

		_buttonOK = (Button) findViewById(R.id.button_dialog_ok);
		_textErrorNumber = (TextView) findViewById(R.id.text_dialog_error_number);

		final int inputType = getIntent().getIntExtra(KEY_INPUT_TYPE, -1);

		String titleString = "";
		String hintInputString = getString(R.string.input_hint_number);

		switch (inputType)
		{
			case INPUT_PRICE:
				titleString = getString(R.string.input_title_price);
				break;
			case INPUT_PRODUCT_AMOUNT:
				titleString = getString(R.string.input_title_amount_increase);
				break;
			case INPUT_PRODUCT_AMOUNT_DESC:
				titleString = getString(R.string.input_title_amount_descrease);
				break;
			case INPUT_DEVICE_NUMBER:
				titleString = getString(R.string.input_title_device_number);
				break;
			case INPUT_USER_AMOUNT:
				_textInput.setInputType(InputType.TYPE_CLASS_TEXT);
				titleString = getString(R.string.input_title_user_amount);
				hintInputString = getString(R.string.input_hint_user_amount);
				break;
			case INPUT_MACHINE_CODE:
				_textInput.setInputType(InputType.TYPE_CLASS_TEXT);
				titleString = getString(R.string.input_title_machine_code);
				hintInputString = getString(R.string.input_hint_machine_code);
				break;
			case INPUT_SERVER_PASSWORD:
				_textInput.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
				titleString = getString(R.string.input_title_server_password);
				hintInputString = getString(R.string.input_hint_password_server);
				break;
			case INPUT_SERVER_USERNAME:
				_textInput.setInputType(InputType.TYPE_CLASS_TEXT);
				titleString = getString(R.string.input_title_server_username);
				hintInputString = getString(R.string.input_hint_username_server);
				break;
			case INPUT_SERVER_DATABASE:
				_textInput.setInputType(InputType.TYPE_CLASS_TEXT);
				titleString = getString(R.string.input_title_server_database);
				hintInputString = getString(R.string.input_hint_database_server);
				break;

			default:

				Utils.debug("Error input type!!!!");
				finish();
				return;
		}

		final boolean isInputText = inputType == INPUT_MACHINE_CODE
				|| inputType == INPUT_SERVER_USERNAME
				|| inputType == INPUT_SERVER_PASSWORD
						|| inputType == INPUT_USER_AMOUNT
								|| inputType == INPUT_SERVER_DATABASE;

		_textTitle.setText(titleString);
		_textInput.setHint(hintInputString);

		_textInput.addTextChangedListener(new TextWatcher()
		{

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3)
			{
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3)
			{
				_textErrorNumber.setVisibility(View.INVISIBLE);
			}

			@Override
			public void afterTextChanged(Editable arg0)
			{

				if (arg0.length() > 0)
				{
					if (!isInputText)
					{

						if (arg0.length() > 5
								&& inputType != INPUT_DEVICE_NUMBER)
						{
							_buttonOK.setEnabled(false);
							_textErrorNumber.setText(getString(R.string.input_error_number_large));
						}
						else
						{
							_buttonOK.setEnabled(true);
						}
					}
				}
				else
				{
					_textErrorNumber.setText(getString(R.string.message_error_input_null));
					_buttonOK.setEnabled(false);
				}

				if (!isInputText)
				{
					try
					{
						Integer.parseInt(arg0.toString());
					} catch (NumberFormatException e)
					{
						e.printStackTrace();
						_buttonOK.setEnabled(false);
						_textErrorNumber.setVisibility(View.VISIBLE);
					}
				}
			}
		});

		_buttonOK.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View arg0)
			{
				Intent intent = new Intent();

				_valueInput = _textInput.getText().toString();

				intent.putExtra(KEY_INPUT_DATA, _valueInput);
				setResult(RESULT_OK, intent);
				finish();
			}
		});

		_timerOutUtils = new TimerOutUtils(this, R.id.text_time, false);
	}

	@Override
	public void onAttachedToWindow()
	{
		if (AppConfig.USE_ATTACH_WINDOW_FOR_DISABLE_HOME_KEY)
		{
			this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
			super.onAttachedToWindow();
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		_timerOutUtils.startTimerNotOperating();

		return super.onTouchEvent(event);
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus)
	{

		switch (AppMain.getInstance().getCodeResult())
		{
			case TIME_OUT_NOT_OPERATOR:
				try
				{
					AppMain.getInstance().setNumberActivityFinishLogout(_numberFinish);
					showDialog(R.string.message_time_out_user_not_operating);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
				break;
			case TIME_OUT_USER:
				try
				{
					AppMain.getInstance().setNumberActivityFinishLogout(_numberFinish);
					showDialog(R.string.message_time_out_user);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
				break;
			default:
				break;
		}

		AppMain.getInstance().setCodeResult(CodeResult.NONE);

		super.onWindowFocusChanged(hasFocus);
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		if (id == R.string.message_time_out_user)
		{
			return new DialogMessageBack(this, true, id, true);
		}
		else if (id == R.string.message_time_out_user_not_operating)
		{

			final DialogMessageConfirm dialogMessageConfirmNotOperating = new DialogMessageConfirm(
					this, false, id, true);
			OnClickListener onClickListenerYESNotOperating = new OnClickListener()
			{

				@Override
				public void onClick(View v)
				{
					dialogMessageConfirmNotOperating.cancel();
					_timerOutUtils.stopTimeOutDialog();
				}
			};
			dialogMessageConfirmNotOperating.setOnclickListenerYes(onClickListenerYESNotOperating);
			return dialogMessageConfirmNotOperating;
		}
		else
			return super.onCreateDialog(id);
	}

	@Override
	protected void onResume()
	{
		_timerOutUtils.startTimerOutUser();
		_timerOutUtils.startTimerNotOperating();
		super.onResume();
	}

	@Override
	protected void onPause()
	{
		_timerOutUtils.stop();
		super.onPause();
	}

}
